﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using XISoft.AppCreator.Util.Common;
using System.Xml;
using System.Windows.Forms;

namespace XISoft.AppCreator.Util.ConfigHelper
{
    class ConfigHelper<E>
    {
        protected List<E> elements = new List<E>();

        private readonly String szPath;

        public ConfigHelper(String filePath)
        {
            szPath = filePath;
            Load();
        }
        /// <summary>
        /// 保存配置
        /// </summary>
        public void Save()
        {
            XmlTextWriter xmlWriter = null;
            try
            {
                xmlWriter = new XmlTextWriter( szPath, Encoding.UTF8);
                
                xmlWriter.Formatting = Formatting.Indented;
                xmlWriter.Indentation = 1;
                xmlWriter.IndentChar = '\t';
                xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                xmlWriter.WriteComment("XISoft Studio设计");
                xmlWriter.WriteComment("程序配置文件");

                XmlSerializer xs = new XmlSerializer(typeof(List<E>));
                xs.Serialize(xmlWriter, elements);


            }
            catch (Exception e)
            {
                LogHelper.Trace(e);
                MessageBox.Show("配置文件不能保存");
            }
            finally
            {
                if (xmlWriter != null)
                {
                    xmlWriter.Flush();
                    xmlWriter.Close();
                }
            }
        }
        /// <summary>
        /// 读取配置文件
        /// </summary>
        public void Load()
        {
            FileStream myFileStream = null;
            try
            {
                // Constructs an instance of the XmlSerializer with the type 
                // of object that is being deserialized. 
                XmlSerializer mySerializer = new XmlSerializer(typeof(List<E>));
                // To read the file, creates a FileStream. 
                myFileStream = new FileStream(szPath, FileMode.OpenOrCreate);
                // Calls the Deserialize method and casts to the object type. 
                elements = mySerializer.Deserialize(myFileStream) as List<E>;
            }
            catch (Exception e)
            {
                LogHelper.Trace(e);
                MessageBox.Show("配置文件有错误");
            }
            finally
            {
                if (myFileStream != null)
                    myFileStream.Close();
            }
        }
        /// <summary>
        /// 添加元素
        /// </summary>
        /// <param name="e"></param>
        public void Add(E e)
        {
            elements.Add(e);
        }
        /// <summary>
        /// 移除元素
        /// </summary>
        /// <param name="e"></param>
        public void Remove(E e)
        {
            elements.Remove(e);
        }
        /// <summary>
        /// 整个配置文件元素
        /// </summary>
        public List<E> Elements
        {
            get { return elements; }
            set { elements = value; }
        }
    }
}
